// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/expression/special_form_registry.h>

namespace kumo::pollux::exec {
    namespace {
        SpecialFormRegistry &specialFormRegistryInternal() {
            static SpecialFormRegistry instance;
            return instance;
        }
    } // namespace

    void SpecialFormRegistry::registerFunctionCallToSpecialForm(
        const std::string &name,
        std::unique_ptr<FunctionCallToSpecialForm> functionCallToSpecialForm) {
        const auto sanitizedName = sanitize_name(name);
        registry_.withWLock([&](auto &map) {
            map[sanitizedName] = std::move(functionCallToSpecialForm);
        });
    }

    void SpecialFormRegistry::unregisterAllFunctionCallToSpecialForm() {
        registry_.withWLock([&](auto &map) { map.clear(); });
    }

    FunctionCallToSpecialForm * MELON_NULLABLE
    SpecialFormRegistry::getSpecialForm(const std::string &name) const {
        const auto sanitizedName = sanitize_name(name);
        FunctionCallToSpecialForm *specialForm = nullptr;
        registry_.withRLock([&](const auto &map) {
            auto it = map.find(sanitizedName);
            if (it != map.end()) {
                specialForm = it->second.get();
            }
        });
        return specialForm;
    }

    std::vector<std::string> SpecialFormRegistry::getSpecialFormNames() const {
        std::vector<std::string> names;
        registry_.withRLock([&](const auto &map) {
            names.reserve(map.size());
            for (const auto &[name, _]: map) {
                names.push_back(name);
            }
        });
        return names;
    }

    const SpecialFormRegistry &specialFormRegistry() {
        return specialFormRegistryInternal();
    }

    SpecialFormRegistry &mutableSpecialFormRegistry() {
        return specialFormRegistryInternal();
    }

    void registerFunctionCallToSpecialForm(
        const std::string &name,
        std::unique_ptr<FunctionCallToSpecialForm> functionCallToSpecialForm) {
        mutableSpecialFormRegistry().registerFunctionCallToSpecialForm(
            name, std::move(functionCallToSpecialForm));
    }

    void unregisterAllFunctionCallToSpecialForm() {
        mutableSpecialFormRegistry().unregisterAllFunctionCallToSpecialForm();
    }

    bool isFunctionCallToSpecialFormRegistered(const std::string &functionName) {
        return mutableSpecialFormRegistry().getSpecialForm(functionName) != nullptr;
    }
} // namespace kumo::pollux::exec
